/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.melhoroferta.persistencia;

import br.com.melhoroferta.entidades.Usuarios;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diego Dantas
 */
public class UsuariosDAO extends DAO {

    public void cadastrarUsuarios(Usuarios u) {
        try {
            openDataBase();
            pstmt = con.prepareStatement("insert into usuarios(nome,apelido,email,cpf,dataNascimento,sexo,telefone,celular,cep,"
                    + "endereco,numero,complemento,bairro,regiao,estado,cidade,login,senha,ativo,ua)"
                    + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");

            pstmt.setString(1, u.getNome());
            pstmt.setString(2, u.getApelido());
            pstmt.setString(3, u.getEmail());
            pstmt.setString(4, u.getCpf());
            pstmt.setDate(5,(java.sql.Date) u.getDataNascimento());
            pstmt.setString(6, u.getSexo());
            pstmt.setString(7, u.getTelefone());
            pstmt.setString(8, u.getCelular());
            pstmt.setString(9, u.getCep());
            pstmt.setString(10, u.getEndereco());
            pstmt.setString(11, u.getNumero());
            pstmt.setString(12, u.getComplemento());
            pstmt.setString(13, u.getBairro());
            pstmt.setString(14, u.getRegiao());
            pstmt.setString(15, u.getEstado());
            pstmt.setString(16, u.getCidade());
            pstmt.setString(17, u.getLogin());
            pstmt.setString(18, u.getSenha());
            pstmt.setInt(19, u.getAtivo());
            pstmt.setDate(20, (java.sql.Date) u.getUa());

            pstmt.execute();
            pstmt.close();

        } catch (Exception e) {
            e.getMessage();
        } finally {
            closeDataBase();
        }

    }

    public List<Usuarios> listarUsuariosAll() {
        List<Usuarios> resp = new ArrayList<Usuarios>();
        try {
            openDataBase();
            pstmt = con.prepareStatement("select * from usuarios");
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Usuarios u = new Usuarios();

                u.setNome(rs.getString("Nome"));
                u.setApelido(rs.getString("Apelido"));
                u.setEmail(rs.getString("Email"));
                u.setCpf(rs.getString("Cpf"));
                u.setDataNascimento(rs.getDate("DataNascimento"));
                u.setSexo(rs.getString("Sexo"));
                u.setTelefone(rs.getString("Telefone"));
                u.setCelular(rs.getString("Celular"));
                u.setCep(rs.getString("Cep"));
                u.setEndereco(rs.getString("Endereco"));
                u.setNumero(rs.getString("Numero"));
                u.setComplemento(rs.getString("Complemento"));
                u.setBairro(rs.getString("Bairro"));
                u.setRegiao(rs.getString("Regiao"));
                u.setEstado(rs.getString("Estado"));
                u.setCidade(rs.getString("Cidade"));
                u.setLogin(rs.getString("Login"));
                u.setSenha(rs.getString("Senha"));
                u.setAtivo(rs.getInt("Ativo"));
                u.setUa(rs.getDate("Ua"));

                resp.add(u);

            }
            rs.close();
            pstmt.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            closeDataBase();
        }
        return resp;
    }

    public Usuarios listaUsuariosByCod(Integer cod) {
        Usuarios u = new Usuarios();

        try {
            openDataBase();
            pstmt = con.prepareStatement("select * from usuarios where NUsuario = ?");
            pstmt.setInt(1, cod);
            rs = pstmt.executeQuery();

            if (rs.next()) {

                u.setNome(rs.getString("Nome"));
                u.setApelido(rs.getString("Apelido"));
                u.setEmail(rs.getString("Email"));
                u.setCpf(rs.getString("Cpf"));
                u.setDataNascimento(rs.getDate("DataNascimento"));
                u.setSexo(rs.getString("Sexo"));
                u.setTelefone(rs.getString("Telefone"));
                u.setCelular(rs.getString("Celular"));
                u.setCep(rs.getString("Cep"));
                u.setEndereco(rs.getString("Endereco"));
                u.setNumero(rs.getString("Numero"));
                u.setComplemento(rs.getString("Complemento"));
                u.setBairro(rs.getString("Bairro"));
                u.setRegiao(rs.getString("Regiao"));
                u.setEstado(rs.getString("Estado"));
                u.setCidade(rs.getString("Cidade"));
                u.setLogin(rs.getString("Login"));
                u.setSenha(rs.getString("Senha"));
                u.setAtivo(rs.getInt("Ativo"));
                u.setUa(rs.getDate("Ua"));

            }
            rs.close();
            pstmt.close();

        } catch (Exception e) {
            e.getMessage();
        } finally {
            closeDataBase();
        }
        return u;
    }

    public void updateUsuario(Usuarios u) {

        try {
            openDataBase();
            pstmt = con.prepareStatement("update usuarios set nome=?,apelido=?,email=?,cpf=?,dataNascimento=?,sexo=?,telefone=?,celular=?,cep=?,"
                    + "endereco=?,numero=?,complemento=?,bairro=?,regiao=?,estado=?,cidade=?,login=?,senha=?,ativo=?,ua=?"
                    + "where NUsuario = ?");

            pstmt.setString(1, u.getNome());
            pstmt.setString(2, u.getApelido());
            pstmt.setString(3, u.getEmail());
            pstmt.setString(4, u.getCpf());
            pstmt.setDate(5, (java.sql.Date) u.getDataNascimento());
            pstmt.setString(6, u.getSexo());
            pstmt.setString(7, u.getTelefone());
            pstmt.setString(8, u.getCelular());
            pstmt.setString(9, u.getCep());
            pstmt.setString(10, u.getEndereco());
            pstmt.setString(11, u.getNumero());
            pstmt.setString(12, u.getComplemento());
            pstmt.setString(13, u.getBairro());
            pstmt.setString(14, u.getRegiao());
            pstmt.setString(15, u.getEstado());
            pstmt.setString(16, u.getCidade());
            pstmt.setString(17, u.getLogin());
            pstmt.setString(18, u.getSenha());
            pstmt.setInt(19, u.getAtivo());
            pstmt.setDate(20, (java.sql.Date) u.getUa());
            pstmt.setInt(21, u.getNusuario());

            pstmt.execute();
            pstmt.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            closeDataBase();
        }

    }

    public void deletarUsuario(Integer cod) {
        try {

            openDataBase();
            pstmt = con.prepareStatement("delete from usuarios where NUsuario = ?");
            pstmt.setInt(1, cod);

            pstmt.execute();
            pstmt.close();

        } catch (Exception e) {
            e.getMessage();
        } finally {
            closeDataBase();
        }
    }
}
